﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using Cesave.DistributedServices.Core.ErrorHandlers;
using Cesave.DistributedServices.PermisosModule.Resources;
using Cesave.Domain.PermisosModule.Contracts.Municipios;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.IoC;
using Cesave.Infrastructure.CrossCutting.Logging;

namespace Cesave.DistributedServices.PermisosModule
{
	public partial class PermisosModuleService
	{
		#region IPermisosModuleService Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="estadoGuid"></param>
		/// <returns></returns>
		public List<Municipio> FindMunicipiosByEstado(Guid estadoGuid)
		{
			try
			{
				IMunicipioService municipioService = IoCFactory.Resolve<IMunicipioService>();
				return municipioService.FindMunicipiosByEstado(estadoGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="estadoGuid"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Municipio> FindMunicipiosByEstadoAndStatus(Guid estadoGuid, bool status = true)
		{
			try
			{
				IMunicipioService municipioService = IoCFactory.Resolve<IMunicipioService>();
				return municipioService.FindMunicipiosByEstadoAndStatus(estadoGuid, status);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="distritoGuid"></param>
		/// <returns></returns>
		public List<Municipio> FindMunicipiosByDistrito(Guid distritoGuid)
		{
			try
			{
				IMunicipioService municipioService = IoCFactory.Resolve<IMunicipioService>();
				return municipioService.FindMunicipiosByDistrito(distritoGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="distritoGuid"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Municipio> FindMunicipiosByDistritoAndStatus(Guid distritoGuid, bool status = true)
		{
			try
			{
				IMunicipioService municipioService = IoCFactory.Resolve<IMunicipioService>();
				return municipioService.FindMunicipiosByDistritoAndStatus(distritoGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="municipioGuid"></param>
		/// <returns></returns>
		public Municipio FindMunicipioByGuid(Guid municipioGuid)
		{
			try
			{
				IMunicipioService municipioService = IoCFactory.Resolve<IMunicipioService>();
				return municipioService.FindMunicipioByGuid(municipioGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="municipio"></param>
		public void AddMunicipio(Municipio municipio)
		{
			try
			{
				IMunicipioService municipioService = IoCFactory.Resolve<IMunicipioService>();
				municipioService.AddMunicipio(municipio);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="municipio"></param>
		public void ChangeMunicipio(Municipio municipio)
		{
			try
			{
				IMunicipioService municipioService = IoCFactory.Resolve<IMunicipioService>();
				municipioService.ChangeMunicipio(municipio);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="municipio"></param>
		public void DeleteMunicipio(Municipio municipio)
		{
			try
			{
				IMunicipioService municipioService = IoCFactory.Resolve<IMunicipioService>();
				municipioService.DeleteMunicipio(municipio);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		#endregion
	}
}
